<?php

/*
  +------------------------------------------------------------------------+
  | Phosphorum                                                             |
  +------------------------------------------------------------------------+
  | Copyright (c) 2013-2014 Phalcon Team and contributors                  |
  +------------------------------------------------------------------------+
  | This source file is subject to the New BSD License that is bundled     |
  | with this package in the file docs/LICENSE.txt.                        |
  |                                                                        |
  | If you did not receive a copy of the license and are unable to         |
  | obtain it through the world-wide-web, please send an email             |
  | to license@phalconphp.com so we can send you a copy immediately.       |
  +------------------------------------------------------------------------+
*/


use Phalcon\Mvc\Model;
use Phalcon\DI;

class CacheableModel extends Model
{
    protected static $_cache = array();
    protected static function di()
    {
        return \Phalcon\DI\FactoryDefault::getDefault();
    }

    protected static function _createKey($parameters)
    {
        $uniqueKey = array();
        foreach ($parameters as $key => $value) {
            if (is_scalar($value)) {
                $uniqueKey[] = $key . ':' . $value;
            } else {
                if (is_array($value)) {
                    $uniqueKey[] = $key . ':[' . self::_createKey($value) .']';
                }
            }
        }
        return join(',', $uniqueKey);
    }

    protected static function _getCache($key)
    {
        $modelCache = self::di()->getModelsCache();
        return $modelCache->get($key);
    }

    protected static function _setCache($key , $result)
    {
        $modelCache = self::di()->getModelsCache();
        $modelCache->save($key , $result);
    }

    public static function find($parameters = null)
    {
        //Create an unique key based on the parameters
        $key = self::_createKey($parameters);
        $result = $this->_getCache($key);
        if (!$result) {
            //Store the result in the memory cache
            $result = parent::find($parameters);
            $this->_setCache($key , $result);

        }
        //Return the result in the cache
        return $result;
    }


    /**
     * Caches models data in memory
     *
     * @param null $parameters
     *
     * @return Model
     */
    public static function findFirst($parameters = null)
    {
        //Create an unique key based on the parameters
        $key = self::_createKey($parameters);
        $key = 'test';
        $result = self::_getCache($key);
        if (!$result) {
            //Store the result in the memory cache
            $result = parent::findFirst($parameters);
            self::_setCache($key , $result);

        }
        //Return the result in the cache
        return $result;
    }

    /**
     * Allows to use the model as a resultset's row
     *
     * @param $value
     *
     * @return $this
     */
    public function setIsFresh($value)
    {
        return $this;
    }

    /**
     * Allows to use the model as a resultset's row
     *
     * @return $this
     */
    public function getFirst()
    {
        return $this;
    }
}
